package com.tz.crm.controller;

import com.alibaba.fastjson.JSON;
import com.tz.crm.entity.Role;
import com.tz.crm.entity.User;
import com.tz.crm.entity.dto.Password;
import com.tz.crm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Set;

/**
 * Created by 戏子入画 on 2017/3/13.${time}
 */
@RequestMapping("/user")
@Controller
public class SysController {

    @Resource
    UserService userService;

    private Password password;

    @RequestMapping(value = "/init")
    public String into() {
        return "initLogin";
    }

    @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/information")
    public String information(HttpServletRequest request) {
        User u = (User) request.getSession().getAttribute("user");
        Set<Role> roles = u.getRoleSet();
        for (Role role : roles) {
            System.out.println("role = " + role.getName());
        }
        return "user/infomation";
    }

    @RequestMapping(value = "/changepassword")
    public String change() {
        return "user/changepassword";
    }

    @RequestMapping(value = "/login")
    public String login(String username, String password, HttpServletRequest request) {
        if (username != null && username != "" && password != null && password != "") {
            String name = username.trim();
            if (userService.isExist("username", name)) {
                //获取 用户帐号是否存在
                User us = userService.getByLogin(name, password);
                //判断密码是否正确
                if (us != null) {
                    //把user对象放入session作用域中
                    request.getSession().setAttribute("user", us);
                    //记住帐号和密码

                    //跳转到首页
                    return "index";
                } else {
                    //密码不正确
                    request.setAttribute("err", "1");
                    return "initLogin";
                }
            } else {
                //账户不正确
                request.setAttribute("err", "0");
                return "initLogin";
            }
        } else {
            request.setAttribute("err", "102");
            return "initLogin";
        }
    }

    //ajax判断 password
    @RequestMapping(value = "changAction", produces = "application/json")
    @ResponseBody
    public String changAction(String password, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        System.out.println("user.getPassword() = " + user.getPassword());
        System.out.println("password = " + password);
        if (user.getPassword().equals(password)) {
            String result = JSON.toJSONString("1");
            return result;
        } else {
            String result = JSON.toJSONString("0");
            return result;
        }
    }

    //修改密码
    @RequestMapping("changsavepassword")
    public String changsavepassword(Password password, HttpServletRequest request) {
        if (password != null) {
            User user = (User) request.getSession().getAttribute("user");
            user.setPassword(password.getNewPassword());
            userService.update(user);
            exitUser(request);
            return "initLogin";
        } else {
            return "index";
        }
    }

    //退出
    @RequestMapping("/exit")
    public String exitUser(HttpServletRequest request) {
        HttpSession httpSession = request.getSession();
        if (httpSession != null) {
            httpSession.invalidate();
        }
        return "initLogin";
    }

}
